<template>
  <div class="ele-body">
    <el-card shadow="never">
      <!-- 搜索表单 -->
      <user-search @search="reload" />
      <!-- 数据表格 -->
      <ele-pro-table
        ref="table"
        :columns="columns"
        :datasource="datasource"
        :selection.sync="selection"
      >
        <!-- 表头工具栏 -->
        <template v-slot:toolbar>
          <el-button
            size="small"
            type="danger"
            icon="el-icon-delete"
            class="ele-btn-icon"
            @click="removeBatch"
          >
            删除
          </el-button>
        </template>

        <template v-slot:balanceNameHeader>
          <span>
            {{ balanceName }}
          </span>
        </template>

        <template v-slot:nickname="{ row }">
          <div class="ele-cell">
            <el-avatar
              v-if="row.avatar"
              :src="row.avatar"
              shape="square"
              :size="40"
            />
            <div class="ele-cell-content">
              <div class="ele-cell-title">{{ row.nickname }} </div>
              <div class="ele-cell-desc">{{ row.telphone }} </div>
            </div>
          </div>
        </template>

        <template v-slot:p_nickname="{ row }">
          <div class="ele-cell" v-if="row.figureUser">
            <el-avatar
              v-if="row.figureUser.avatar"
              :src="row.figureUser.avatar"
              shape="square"
              :size="40"
            />
            <div class="ele-cell-content">
              <div class="ele-cell-title">{{ row.figureUser.nickname }} </div>
              <div class="ele-cell-desc">{{ row.figureUser.telphone }} </div>
            </div>
          </div>
          <span v-else>无</span>
        </template>

        <ele-modal
          width="80%"
          :close-on-click-modal="false"
          custom-class="ele-dialog-form"
          :title="userName + balanceName"
          :visible.sync="showBalance"
        >
    
        </ele-modal>

        <!-- 状态列 -->
        <template v-slot:is_freeze="{ row }">
          <el-switch
            :active-value="1"
            :inactive-value="2"
            v-model="row.is_freeze"
            @change="editStatus(row)"
          />
        </template>
           <template v-slot:platformInfo="{ row }">
          <div class="ele-cell" v-if="row.platformInfo">
            <div class="ele-cell-content">
              <div class="ele-cell-title">{{ row.platformInfo.name }} </div>
              <div class="ele-cell-desc">{{ row.platformInfo.telphone }} </div>
            </div>
          </div>
          <span v-else>无</span>
        </template>

        <template v-slot:qrcode="{ row }">
          <el-image
            style="width: 70px; height: 70px"
            :src="row.qrcode"
            fit="fit"
          >
          </el-image>
          <el-link
            type="primary"
            style="margin-left: 5px"
            :underline="false"
            @click="reloadQrcode(row)"
          >
            重新生成
          </el-link>
        </template>

        <!-- 操作列 -->
        <template v-slot:action="{ row }">
          
      

          <el-popconfirm
            class="ele-action"
            title="确定要删除此数据吗？"
            @confirm="remove(row)"
          >
            <template v-slot:reference>
              <el-link type="danger" :underline="false" icon="el-icon-delete">
                删除
              </el-link>
            </template>
          </el-popconfirm>
        </template>
      </ele-pro-table>
    </el-card>
    <!-- 编辑弹窗 -->
  
  </div>
</template>

<script>
import UserSearch from './components/user-search';

import { userList, remove, modify, resetPassword } from '@/api/user/list';

export default {
  name: 'User',
  components: {
    UserSearch
  },
  computed: {
    balanceName() {
      return this.$store.state.user.balance_name;
    }
  },
  data() {
    return {
      // 表格列配置
      columns: [
        {
          columnKey: 'selection',
          type: 'selection',
          width: 45,
          align: 'center',
          fixed: 'left'
        },
        {
          columnKey: 'index',
          type: 'index',
          width: 45,
          align: 'center',
          showOverflowTooltip: true,
          fixed: 'left'
        },
           {
          prop: 'platformInfo',
          label: '平台详情',
          showOverflowTooltip: true,
          minWidth: 110,
          slot: 'platformInfo',
          show: localStorage.getItem('platform_id') == 0 ? true: false ,

        },
        {
          prop: 'nickname',
          label: '昵称',
          showOverflowTooltip: true,
          minWidth: 155,
          slot: 'nickname'
        },
 
        {
          prop: 'create_time',
          label: '创建时间',
          align: 'center',
          showOverflowTooltip: true,
          minWidth: 100
        },
        {
          columnKey: 'action',
          label: '操作',
          width: 350,
          align: 'center',
          resizable: false,
          slot: 'action',
          fixed: 'right',
          hideInSetting: true,
          showOverflowTooltip: true
        }
      ],
      // 表格选中数据
      selection: [],
      // 当前编辑数据
      current: null,
      // 是否显示编辑弹窗
      showEdit: false,
      // 是否显示积分弹窗
      showBalance: false,
      // 是否显示积分弹窗
      showBalance: false,
      // 是否显示团队弹窗
      showTeam: false,
      //弹框名称
      id: 0,
      userName: ''
    };
  },
  methods: {
    beforeHandleCommand(index, row, command) {
      return {
        index: index,
        row: row,
        command: command
      };
    },
    onDropClick(command) {
      switch (command.command) {
        case 'a': //编辑
          this.openAddNum(command.row, 1);
          break;
        case 'b': //充值密码
          this.openAddNum(command.row, 2);
          break;
        case 'c': //删除
          this.openAddNum(command.row, 3);
          break;
        case 'd': //删除
          this.openAddNum(command.row, 4);
          break;
      }
    },
    /* 表格数据源 */
    datasource({ page, limit, where, order }) {
      return userList({ ...where, ...order, page, limit });
    },
    /* 刷新表格 */
    reload(where) {
      this.$refs.table.reload({ page: 1, where: where });
    },
    /* 打开编辑弹窗 */
    openEdit(row) {
      this.current = row;
      this.showEdit = true;
    },

    /* 打开编辑弹窗 */
    openCheck(row) {
      this.id = row.id;
      this.current = row;
      this.showCheck = true;
    },

    /* 打开编辑弹窗 */
    openBalance(row) {
      this.id = row.id;
      this.current = row;
      this.showBalance = true;
      this.userName = row.nickname;
    },
    /* 打开编辑弹窗 */
    openBalance(row) {
      this.id = row.id;
      this.current = row;
      this.showBalance = true;
      this.userName = row.nickname;
    },
    /* 打开编辑弹窗 */
    openTeam(row) {
      this.id = row.id;
      this.current = row;
      this.showTeam = true;
      this.userName = row.nickname;
    },
    /* 删除 */
    remove(row) {
      const loading = this.$loading({ lock: true });
      remove(row.id)
        .then((msg) => {
          loading.close();
          this.$message.success(msg);
          this.reload();
        })
        .catch((e) => {
          loading.close();
          this.$message.error(e.message);
        });
    },
    /* 更新二维码 */
    reloadQrcode(row) {
      const loading = this.$loading({ lock: true });
      reloadQrcode({ user_id: row.userId })
        .then((msg) => {
          loading.close();
          this.$message.success(msg);
          this.reload();
        })
        .catch((e) => {
          loading.close();
          this.$message.error(e.message);
        });
    },
    /* 重置合伙人密码 */
    resetPsw(row) {
      this.$confirm('确定要重置此用户的密码为"123456"吗?', '提示', {
        type: 'warning'
      })
        .then(() => {
          const loading = this.$loading({ lock: true });
          resetPassword(row.id)
            .then((msg) => {
              loading.close();
              this.$message.success(msg);
            })
            .catch((e) => {
              loading.close();
              this.$message.error(e.message);
            });
        })
        .catch(() => {});
    },

    /* 批量删除 */
    removeBatch() {
      if (!this.selection.length) {
        this.$message.error('请至少选择一条数据');
        return;
      }
      this.$confirm('确定要删除选中的合伙人吗?', '提示', {
        type: 'warning'
      })
        .then(() => {
          const loading = this.$loading({ lock: true });
          remove(this.selection.map((d) => d.id))
            .then((msg) => {
              loading.close();
              this.$message.success(msg);
              this.reload();
            })
            .catch((e) => {
              loading.close();
              this.$message.error(e.message);
            });
        })
        .catch(() => {});
    },

    /* 更改状态 */
    editStatus(row) {
      const loading = this.$loading({ lock: true });
      modify({ id: row.id, field: 'is_freeze', value: row.is_freeze })
        .then((msg) => {
          loading.close();
          this.$message.success(msg);
        })
        .catch((e) => {
          loading.close();
          row.is_freeze = row.is_freeze == 2 ? 1 : 2;
          this.$message.error(e.message);
        });
    }
  }
};
</script>

<style lang="scss" scoped>
.ele-cell-content {
  overflow: hidden;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
}
</style>
